package flexo.gui.flat;

import flexo.graphic.Color;
import flexo.graphic.GraphicSystem;
import flexo.graphic.Material;
import flexo.gui.Button;
import flexo.gui.Cursor;
import flexo.math.Coords;

public class Theme extends flexo.gui.Theme 
{
	protected static final String sMatPanelID = "GUI_Theme_Flat_Panel";
	protected static final String sMatWidgetID = "GUI_Theme_Flat_Widget";
	protected static final String sMatWidgetOverID = "GUI_Theme_Flat_Widget_Over";
	protected static final String sMatCursorID = "GUI_Theme_Flat_Cursor";
	
	protected String matPanelID;
	protected String matWidgetID;
	protected String matWidgetOverID;
	protected String matCursorID;
	
	private GraphicSystem gs = (GraphicSystem)flexo.Kernel.getSystem(GraphicSystem.ID);	
	
	protected void createMaterials(Color panel, 
			Color widget, Color widgetOver, 
			Color cursor, Color font)
	{
		// Panel
		matPanelID = sMatPanelID + '[' + getName() + ']';
		Material mat = gs.addMaterial(matPanelID,"oneColor");
		mat.setBlending(flexo.graphic.Material.Blending.TRANSPARENT_ALPHA);
		mat.grab();
		mat.getShaderParam("color").setValue(panel);
		
		// Widget
		matWidgetID = sMatWidgetID + '[' + getName() + ']';
		mat = gs.addMaterial(matWidgetID,"oneColor");
		mat.grab();
		mat.getShaderParam("color").setValue(widget);
		
		// Widget over
		matWidgetOverID = sMatWidgetOverID + '[' + getName() + ']';
		mat = gs.addMaterial(matWidgetOverID,"oneColor");
		mat.grab();
		mat.getShaderParam("color").setValue(widgetOver);
		
		// Cursor
		matCursorID = sMatCursorID + '[' + getName() + ']';
		mat = gs.addMaterial(matCursorID,"oneColor");
		mat.grab();
		mat.getShaderParam("color").setValue(cursor);
	}
	
	public Theme(String name)
	{
		super(name);
		createMaterials(new Color(0,0,0,0.8f),
				new Color(0.3f,0.3f,0.3f),
				new Color(0.4f,0.5f,0.9f),
				Color.RED,
				Color.WHITE);	
	}
	
	public Theme(String name, Color panel, Color widget, Color widgetOver, Color cursor, Color font)
	{
		super(name);
		createMaterials(panel,widget,widgetOver,cursor,font);
	}
	
	@Override
	public flexo.gui.Panel createPanel(String name, Coords size)
	{
		return new flexo.gui.flat.Panel(name,size,matPanelID);
	}

	@Override
	public Button createButton(String name, Coords size, String text)
	{
		return new flexo.gui.flat.Button(name,size, matWidgetID, matWidgetOverID );
	}

	@Override
	public Cursor createCursor(Coords size, Coords hotspot)
	{
		return new flexo.gui.flat.Cursor(size,hotspot,matCursorID);
	}		

	@Override
	protected void destroy()
	{
		gs.getMaterial(matCursorID).remove();
		gs.getMaterial(matPanelID).remove();
		gs.getMaterial(matWidgetID).remove();
		gs.getMaterial(matWidgetOverID).remove();
	}
}
